package com.xbongbong.config;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.parent.interfaces.BusinessBatchEditService;
import com.xbongbong.parent.interfaces.BusinessBatchTeamOperateService;
import com.xbongbong.parent.interfaces.BusinessDeleteService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2022/11/14 11:12
 */
@Component
public class BatchOperateConfig implements ApplicationContextAware {

    private Map<String, BusinessBatchTeamOperateService> businessBatchTeamOperateServiceMap;

    private Map<String, BusinessBatchEditService> businessBatchUpdateServiceMap;

    private Map<String, BusinessDeleteService> businessDeleteMap;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        businessBatchTeamOperateServiceMap = applicationContext.getBeansOfType(BusinessBatchTeamOperateService.class);
        businessBatchUpdateServiceMap = applicationContext.getBeansOfType(BusinessBatchEditService.class);
        businessDeleteMap = applicationContext.getBeansOfType(BusinessDeleteService.class);
    }

    @Bean("businessBatchTeamOperateServiceMap")
    public Map<Integer, BusinessBatchTeamOperateService> initBatchTeam() {
        Map<Integer, BusinessBatchTeamOperateService> result = new HashMap<>();
        businessBatchTeamOperateServiceMap.forEach((key, item) -> item.businessType().forEach(v -> result.put(v, item)));
        return result;
    }

    @Bean("businessBatchUpdateServiceMap")
    public Map<Integer, BusinessBatchEditService> initBatchUpdate() {
        Map<Integer, BusinessBatchEditService> result = new HashMap<>();
        businessBatchUpdateServiceMap.forEach((key, item) -> {
            item.businessType().forEach(v->{
                result.put(Integer.valueOf(v.toString()), item);
            });

        });
        return result;
    }

    /**
     * 删除
     * @return
     */
    @Bean("businessDeleteMap")
    public Map<Integer, BusinessDeleteService> businessDeleteMap() {
        Map<Integer, BusinessDeleteService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.businessDeleteMap.forEach((key, item)->{
            item.businessType().forEach(type->resultMap.put(Integer.valueOf(type.toString()), item));
        });
        return resultMap;
    }
}
